/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Core;

import Common.DateAndTime;
import Common.Log;
import java.io.IOException;
import java.util.*;
import Database.*;


/**
 *
 * @author harish
 */
public class Processor
{
    private static Processor _instance =null;
    public static List<Data> _data=new ArrayList<Data>();;
    private List<SmartHome> _smartHomes=null;
    private String _currentDateTime=DateAndTime.GetDateTime();
    public static Calendar DateTime=Calendar.getInstance();
    private Processor(){}

    public static Processor GetInstance()
    {
        if(_instance==null)
        {
            _instance=new Processor();
        }
        return _instance;
    }
    
    public void Start()
    {
        try
        {
            _smartHomes=new ArrayList<SmartHome>();
            CreateHomes();
            StartTimeScaling();
        }        
        catch(Exception e)
        {
            
            try
            {
            Log.GetInstance().Write("Error in starting the processor");
            }
            catch(IOException i)
            {
                  
            }
        }
    }
    
    private void CreateHomes()
    {
        try
        {
            for(int i=0;i<Configuration.GetInstance().GetHomes();i++)
            {
                SmartHome sm=new SmartHome();       
                _smartHomes.add(sm);
            }
        }
        catch(Exception e)
        {            
            try
            {
            Log.GetInstance().Write("Error in creating homes");
            }
            catch(IOException i)
            {
                  
            }
        }
    }      
    
    public void StartTimeScaling()
    {
        try
        {
            Timer t=new Timer();
            t.schedule(new Schedule(), 1000*10);
        }        
        catch(Exception e)
        {
            
            try
            {
                Log.GetInstance().Write("Error in time scaling");
            }
            catch(IOException i)
            {
                  
            }
        }
    }
    
    public void InsertData()
    {
        for(SmartHome sh: _smartHomes)
        {            
            sh.InsertData();
            Database.GetInstance().InsertData(_data);
        }
    }
}
